{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Elegently set object's properties"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(<__main__.A object at 0x03537750>,)\n",
      "()\n"
     ]
    }
   ],
   "source": [
    "class A:\n",
    "    def printf(*args):\n",
    "        print(args)\n",
    "\n",
    "def printff(*args):\n",
    "    print(args)\n",
    "\n",
    "a=A()\n",
    "a.printf()\n",
    "a.printf=printff\n",
    "a.printf()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "58\n"
     ]
    }
   ],
   "source": [
    "class A:\n",
    "    def __init__(self,q,w,e,r):\n",
    "        self.__dict__.update(locals())\n",
    "        del self.__dict__['self']\n",
    "        print(self.r)\n",
    "a=A(1,5,6,58)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hi,my score is 99\n",
      "Hi,my score is 56\n"
     ]
    }
   ],
   "source": [
    "class B:\n",
    "    _score=99\n",
    "    @property\n",
    "    def score(self):\n",
    "        return self._score\n",
    "    @score.setter\n",
    "    def score(self,n):\n",
    "        if 0<=n<=100:\n",
    "            self._score=n\n",
    "        return\n",
    "    def __call__(self):\n",
    "        print('Hi,my score is %d'%self.score)\n",
    "\n",
    "b=B()\n",
    "b()\n",
    "b.score=56\n",
    "#print(b.score)\n",
    "b()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
